package com.sport.utils;

import com.google.gson.Gson;
import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.BucketManager;
import com.qiniu.storage.Configuration;
import com.qiniu.storage.Region;
import com.qiniu.storage.UploadManager;
import com.qiniu.storage.model.DefaultPutRet;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

@org.springframework.context.annotation.Configuration
@Slf4j
public class QiniuUtils {

    private String accessKey="PsZmSW8Y1IoMAK7VWFMKrgvADgrNkkPBNEZp9mVR";
    private String secretKey="N1E2MMOqHohYms4HKtPP-DXF7ytXocAES5AwgtE3";

    private String bucket="java-sport";


    /**
     * 根据文件路径和文件名上传
     */


    public String upload(String filePath, String fileName) {
//根据存储桶选择的地域进行选择Region对象,放入配置类

        Configuration cfg = new Configuration(Region.region2());
        UploadManager uploadManager = new UploadManager(cfg);
//        鉴权
        Auth auth = Auth.create(accessKey, secretKey);

        String uploadToken = auth.uploadToken(bucket);

        String name = this.getName(fileName);
        try {
            Response response = uploadManager.put(filePath, fileName, uploadToken);
            DefaultPutRet putRet=new Gson().fromJson(response.bodyString(),DefaultPutRet.class);
            return name;
        } catch (QiniuException e) {
            Response r = e.response;
            try {
                log.error("文件上传失败==={}", r.bodyString());
            } catch (QiniuException exception) {
                //ignore
            }
            return null;
        }


    }

    public String getName(String fileName) {
        SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
        return format.format(new Date() + fileName);
    }


    /**
     * 根据字节流上传文件
     *
     * @param bytes
     * @param fileName
     * @return
     */
    public String upload(byte[] bytes, String fileName) {
//根据存储桶选择的地域进行选择Region对象,放入配置类

        Configuration cfg = new Configuration(Region.region2());
        UploadManager uploadManager = new UploadManager(cfg);
//        鉴权
        Auth auth = Auth.create(accessKey, secretKey);

        String uploadToken = auth.uploadToken(bucket);

        String name = this.getName(fileName);
        System.out.println("qinniuUtils的name=="+name);
        try{
            Response response = uploadManager.put(bytes, fileName, uploadToken);
            DefaultPutRet putRet=new Gson().fromJson(response.bodyString(),DefaultPutRet.class);
            return name;
        } catch (QiniuException e) {
            Response r = e.response;
            try {
                log.error("文件上传失败==={}", r.bodyString());
            } catch (QiniuException exception) {
                //ignore
            }
            return null;
        }


    }

    /**
     * 根据流上传文件
     * @param stream
     * @param fileName
     * @return
     */
    public String upload(InputStream stream, String fileName) {
//根据存储桶选择的地域进行选择Region对象,放入配置类
        System.out.println("进入upload方法");
        Configuration cfg = new Configuration(Region.region2());
        System.out.println("cfg======"+cfg);
        UploadManager uploadManager = new UploadManager(cfg);
        System.out.println("uploadMangager===="+uploadManager);
//        鉴权
        Auth auth = Auth.create(accessKey, secretKey);
        System.out.println("auth===>"+auth);
        String uploadToken = auth.uploadToken(bucket);
        System.out.println("uploadToken"+uploadToken);
        String name = this.getName(fileName);
        System.out.println("获取文件的名字"+name);
        try {
            Response response = uploadManager.put(stream, fileName, uploadToken,null,null);
//            解析上传成功的结果
            DefaultPutRet putRet=new Gson().fromJson(response.bodyString(),DefaultPutRet.class);
            log.info("文件上传成功==={}",putRet);
            return name;
        } catch (QiniuException e) {
            Response r = e.response;
            try {
                log.error("文件上传失败==={}", r.bodyString());
            } catch (QiniuException exception) {
                //ignore
            }
            return null;
        }

    }
public void delete(String fileName){
//        构造一个带指定Region对象的配置类
    Configuration cfg =new Configuration(Region.region2());

    Auth auth=Auth.create(accessKey,secretKey);
    BucketManager bucketManager=new BucketManager(auth,cfg);

    try{
        bucketManager.delete(bucket,fileName);
        log.info("删除成功");

    } catch (QiniuException exception) {
    log.info("删除失败==={}",exception.getMessage());
    log.error(exception.response.toString());
    }
}


    }


